from app import db

# 体检套餐模型
class Package(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128), unique=True, nullable=False)
    description = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return f'<Package {self.name}>'
    
    def __init__(self, name: str, description: str=None):
        self.name = name
        self.description = description
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description
    }
    def modify(self, name: str=None, description: str=None):
        if name:
            self.name = name
        if description:
            self.description = description

# 体检项目模型
class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return f'<Item {self.content}>'
    
    def __init__(self, content: str):
        self.content = content
    def to_dict(self):
        return {
            'id': self.id,
            'content': self.content
    }

    def modify(self, content: str=None):
        if content:
            self.content = content


# 体检套餐和体检项目的关联表模型
class PackageItem(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    package_id = db.Column(db.Integer, db.ForeignKey('package.id'), nullable=False)
    item_id = db.Column(db.Integer, db.ForeignKey('item.id'), nullable=False)

    def __repr__(self):
        return f'<PackageItem {self.package_id}, {self.item_id}>'
    
    def __init__(self, package_id, item_id):
        self.package_id = package_id
        self.item_id = item_id
    def to_dict(self):
        return {
            'id': self.id,
            'package_id': self.package_id,
            'item_id': self.item_id
        }
    

# 实际体检预约模型
class Appointment(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    patient_id = db.Column(db.Integer, db.ForeignKey('patient.id'), nullable=False)
    package_id = db.Column(db.Integer, db.ForeignKey('package.id'), nullable=False)
    appointment_time = db.Column(db.DateTime, nullable=False)
    doctor_id = db.Column(db.Integer, db.ForeignKey('doctor.id'))
    status = db.Column(db.String(64), nullable=False)
    result = db.Column(db.Text)

    def __repr__(self):
        return f'<Appointment {self.id}>'
    def __init__(self, patient_id, package_id, appointment_time, status):
        self.patient_id = patient_id
        self.package_id = package_id
        self.appointment_time = appointment_time
        self.status = status

    def to_dict(self):
        return {
            'id': self.id,
            'patient_id': self.patient_id,
            'package_id': self.package_id,
            'appointment_time': self.appointment_time,
            'doctor_id': self.doctor_id,
            'status': self.status,
            'result': self.result
        }
    
    def modify(self, patient_id=None, package_id=None, appointment_time=None, doctor_id=None, status=None, result=None):
        if patient_id:
            self.patient_id = patient_id
        if package_id:
            self.package_id = package_id
        if appointment_time:
            self.appointment_time = appointment_time
        if doctor_id:
            self.doctor_id = doctor_id
        if status:
            self.status = status
        if result:
            self.result = result